package org.example.linkedlist;

import org.example.Main;

//反转链表
public class demo2 {


     static class ListNode {
          int val;
         ListNode next;
         ListNode() {}
          ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
      }
    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        // 这里使用头插法
        ListNode node1=head.next;
        head.next = null;
        ListNode node2=null;
        while(node1!=null){
            node2=node1.next;
            node1.next=head;
            head=node1;
            node1=node2;
        }
        return head;
    }

    public static void main(String[] args) {

    }
}
